public class Solution2239 {
    public int findClosestNumber(int[] nums) {
        int max=Integer.MAX_VALUE,ans=-1;
        for (int num : nums) {
            if (Math.abs(num)<max || Math.abs(num)==max && ans<0 && num>0){
                max=Math.abs(num);
                ans=num;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution2239().findClosestNumber(new int[]{-4,-2,1,4,8}));
    }
}
